﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.Vector;

import com.baidu.mobstat.StatService;
import com.thorqq.magictimer.db.DBHelper;
import com.thorqq.magictimer.download.DownloadProxy;
import com.thorqq.magictimer.timeLoop.QuickSetting;
import com.thorqq.magictimer.timeLoop.Timer;
import com.thorqq.magictimer.timeLoop.TimerMgr;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.widget.ListViewNoScroll;
import com.thorqq.magictimer.widget.slidingmenu.SlidingMenu;
import com.umeng.fb.NotificationType;
import com.umeng.fb.UMFeedbackService;
import com.wanpu.pay.PayConnect;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MagicTimerActivity extends BaseSlidingFragmentActivity implements MsgReceiver
{
    private static final int REQUEST_CODE_TIMER_SETTING = 1;

    private static final int DEFAULT_INTERNAL_TO_EXIT = 2000;
    private static final int BACKGROUND_TASK_DELAY = 2000;
    public static final String BAIDU_API_KEY = "xd8VHx0w4o3WBY4BIx3m6LZY";

    private final String LIST_TEXT = "text";

    private ArrayList<Map<String, Object>> mTimerItemList = new ArrayList<Map<String, Object>>();
    private AdapterTimerItem mTimerItemAdapter;
    private LayoutInflater mInflater;

    private View mTimerItemHeader;
    private ImageView mHelpOpenSysMenu;
    private ListViewNoScroll mTimerListview;
    private View mAddNewTimerLayout;
    private ImageButton mBtnConfig;
    private TextView mTvNoTimer;
    
    //系统菜单
    private SlidingMenu mSlidingMenu;
    private ListView mMenuListView;
    private SimpleAdapter mMenuAdapter;

    //timer上下文菜单
    private View mTimerMenuView;
    private TextView mTimerMenuExec;
    private TextView mTimerMenuEdit;
    private TextView mTimerMenuDelete;
    private PopupWindow mTimerMenu;
    private final int mTimerMenuHeight = 200;
    private int mCurrentTimerPos;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initSlidingMenu();
        setContentView(R.layout.main);

        mInflater = getLayoutInflater();

        Util.log("MagicTimerActivity start");
        
        // 启动闹铃
        TimerMgr.setMsgReceiver(this);
                
        //初始化下载器
        DownloadProxy.init(this);
        
        initLayout();
        updateLayout();
        registerListener();

        initPay();
        delayInit();                

        Util.log("MagicTimerActivity onCreate success");
    }
    
    private void initPay()
    {
        ApplicationInfo appInfo;
        try
        {
            appInfo = this.getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
            String appId = appInfo.metaData.getString("APP_ID");
            String baiduChannel = appInfo.metaData.getString("BaiduMobAd_CHANNEL");
            
            PayConnect.getInstance(appId, baiduChannel, this);
            
        } catch (NameNotFoundException e)
        {
            Util.log_ex(e);
        }
    }
    
    //把一些不重要的东西延迟初始化
    private void delayInit()
    {
        new Handler().postDelayed(new Runnable() 
        {
            public void run() 
            {
                //通知栏提醒有新的回复
                UMFeedbackService.enableNewReplyNotification(MagicTimerActivity.this, 
                        NotificationType.NotificationBar);

                // 检查更新
                //UpdateManager manager = new UpdateManager(this);
                //manager.checkUpdate(UpdateManager.SHOW_NEED_UPDATE_NOTICE_NO);
                if(Configuration.getIntance().isCheckUpdate())
                {
                    UmengUpdateManager.getIntance().checkUpdateSilence(MagicTimerActivity.this);
                }
            }

        }, BACKGROUND_TASK_DELAY);

    }

    private void initSlidingMenu() {
        setBehindContentView(R.layout.behind_slidingmenu);
        // customize the SlidingMenu
        mSlidingMenu = getSlidingMenu();
        mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
        mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // sm.setFadeDegree(0.35f);
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        mSlidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        mSlidingMenu.setShadowWidth(20);
        // sm.setBehindScrollScale(0.333f);
    }

    public void startDonationActivity()
    {
        Intent i = new Intent(this, DonationActivity.class);
        startActivity(i);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void startTimerSettingActivity(Timer timer, int request_code)
    {
        Intent i = new Intent(this, SettingActivityTimer.class);
        i.putExtra(TimerMgr.ALARM_INTENT_EXTRA, timer);
        startActivityForResult(i, request_code);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void startConfigurationActivity()
    {
        Intent i = new Intent(this, ConfigurationActivity.class);
        startActivity(i);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    private HashMap<String, Object> makeMap(Timer timer)
    {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("timer", timer);

        map.put("listenerNameInfo", new ListenerNameInfo(this, timer));
        map.put("listenerTime", new ListenerTime(this, timer));
        map.put("listenerMore", new ListenerMore(this, timer));
        map.put("longListener", new TimerLongClickListener(this, timer));

        map.put("childView", new ChildViewTimerItem(this, timer, this));
        map.put("visibility", View.GONE);

        return map;
    }
    
    private void initLayout()
    {
        // Timer item list view
        mTimerListview = (ListViewNoScroll) findViewById(R.id.ListViewTimerItem);
        mTimerListview.setDivider(this.getResources().getDrawable(R.drawable.gradient_shape_hor));
        mTimerListview.setDividerHeight(1);

        mTimerItemAdapter = new AdapterTimerItem(this, mTimerItemList);
        mTimerListview.setAdapter(mTimerItemAdapter);

        mTimerItemHeader = mInflater.inflate(R.layout.timer_item_header, null);
        mTimerListview.addHeaderView(mTimerItemHeader);

        mTvNoTimer = (TextView) findViewById(R.id.tvNoTimer);
        mTimerListview.setEmptyView(mTvNoTimer);

        mBtnConfig = (ImageButton) mTimerItemHeader.findViewById(R.id.ivTimerItemHeaderImage);
        mHelpOpenSysMenu = (ImageView) mTimerItemHeader.findViewById(R.id.ivHelpOpenSysMenu);
        mAddNewTimerLayout = (View) findViewById(R.id.linearLayoutAddNewTimer);
        
        //系统菜单
        mMenuListView = (ListView) findViewById(R.id.behind_list_show);
        initListView();
        
        //上下文菜单
        mTimerMenuView = getLayoutInflater().inflate(R.layout.menu_timer, null, false);
        mTimerMenu = new PopupWindow(mTimerMenuView, 
                LinearLayout.LayoutParams.WRAP_CONTENT, 
                LinearLayout.LayoutParams.WRAP_CONTENT,
                true);
        mTimerMenuExec = (TextView) mTimerMenuView.findViewById(R.id.tvTimerMenuExec);
        mTimerMenuEdit    = (TextView) mTimerMenuView.findViewById(R.id.tvTimerMenuEdit);
        mTimerMenuDelete  = (TextView) mTimerMenuView.findViewById(R.id.tvTimerMenuDelete);
    }

    private void initListView() {
        mMenuAdapter = new SimpleAdapter(this, getData(),R.layout.behind_list_show, 
                new String[] { LIST_TEXT},
                new int[] { R.id.textview_behind_title}) ;
        mMenuListView.setAdapter(mMenuAdapter);
        mMenuListView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {                
                switch(position)
                {
                case 0: 
                    startConfigurationActivity();
                    break;
                case 1: 
                    Intent i1 = new Intent(MagicTimerActivity.this, TimerLogActivity.class);
                    startActivity(i1);
                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                    break;
                case 2: 
                    Intent i2 = new Intent(MagicTimerActivity.this, DownloadActivity.class);
                    startActivity(i2);
                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                    break;
                case 3: 
                    UMFeedbackService.setGoBackButtonVisible();
                    UMFeedbackService.openUmengFeedbackSDK(MagicTimerActivity.this);
                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                    break;
                case 4: 
                    Intent i4 = new Intent(MagicTimerActivity.this, HelpActivity.class);
                    startActivity(i4);
                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                    break;
                case 5: 
                    startDonationActivity();
                    break;
                default:
                    break;
                }
                
            }
        });

    }
    
    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(LIST_TEXT, getResources().getString(R.string.main_menu_config));
//        map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_handpick);
        list.add(map);
        map = new HashMap<String, Object>();
        map.put(LIST_TEXT, getResources().getString(R.string.main_menu_log));
//        map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_news);
        list.add(map);
        map = new HashMap<String, Object>();
        map.put(LIST_TEXT, getResources().getString(R.string.main_menu_download));
//        map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_studio);
        list.add(map);
        map = new HashMap<String, Object>();
        map.put(LIST_TEXT, getResources().getString(R.string.main_menu_feedback));
//        map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_blog);
        list.add(map);
        map = new HashMap<String, Object>();
        map.put(LIST_TEXT, getResources().getString(R.string.main_menu_help));
//      map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_blog);
        list.add(map);
        map = new HashMap<String, Object>();
        map.put(LIST_TEXT, getResources().getString(R.string.main_menu_donation));
//      map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_blog);
        list.add(map);
        return list;
    }

    private void updateLayout()
    {
        // 仅在第一次启动的时候抖动，提示用户可以点击。等用户按下之后，以后永远都不再抖动
        // mBtnConfig 动画
        if (Configuration.getIntance().isFirstStart())
        {
            Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
            shake.reset();
            shake.setFillAfter(true);
            mBtnConfig.startAnimation(shake);
            
            mHelpOpenSysMenu.setVisibility(View.VISIBLE);
        }

        // 定时器列表
        mTimerItemList.clear();
        Vector<Timer> timers = TimerMgr.getAllTimers();

        for (Timer timer : timers)
        {
            mTimerItemList.add(this.makeMap(timer));
        }

        mTimerItemAdapter.notifyDataSetChanged();
        
        //系统菜单
        initPopupWindow();

    }

    private void updateTimerItemLayout(Timer timer)
    {
        int i = 0;
        for (; i < mTimerItemList.size(); i++)
        {
            Timer t = (Timer) mTimerItemList.get(i).get("timer");
            if (t.getID() == timer.getID())
            {
                mTimerItemList.get(i).put("timer", timer);
                break;
            }
        }

        if (i == mTimerItemList.size())
        {
            mTimerItemList.add(this.makeMap(timer));
        }

        mTimerItemAdapter.notifyDataSetChanged();
    }

    private void registerListener()
    {
        //打开系统菜单的提示信息
        mHelpOpenSysMenu.setOnClickListener(new LinearLayout.OnClickListener()
        {
            public void onClick(View v)
            {
                mHelpOpenSysMenu.setVisibility(View.GONE);
                mBtnConfig.clearAnimation();
                Configuration.getIntance().setFirstStart();
            }

        });
        
        //弹出系统菜单
        mBtnConfig.setOnClickListener(new LinearLayout.OnClickListener()
        {
            public void onClick(View v)
            {
                toggle();
                
                mHelpOpenSysMenu.setVisibility(View.GONE);
                
                mBtnConfig.clearAnimation();
                Configuration.getIntance().setFirstStart();
            }

        });

        // timer菜单 点击其他地方消失
        mTimerMenuView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (mTimerMenu != null && mTimerMenu.isShowing())
                {
                    mTimerMenu.dismiss();
                }
                return false;
            }

        });   
        //timer 菜单：立即执行
        mTimerMenuExec.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Timer timer = TimerMgr.getAllTimers().get(mCurrentTimerPos);
                Intent doAction = new Intent(TimerMgr.ACTION_MAIN);
                doAction.putExtra(TimerMgr.ALARM_INTENT_EXTRA, timer);
                MagicTimerActivity.this.startService(doAction);
                
                mTimerMenu.dismiss();
                mCurrentTimerPos = -1;
            }
        });
        //timer 菜单：编辑
        mTimerMenuEdit.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Timer timer = TimerMgr.getAllTimers().get(mCurrentTimerPos);
                
                startTimerSettingActivity(timer, REQUEST_CODE_TIMER_SETTING);
                
                mTimerMenu.dismiss();
                mCurrentTimerPos = -1;
            }
        });
        //timer 菜单：删除
        mTimerMenuDelete.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Timer timer = TimerMgr.getAllTimers().get(mCurrentTimerPos);
                deleteTimer(timer.getID());
                
                mTimerMenu.dismiss();
                mCurrentTimerPos = -1;
            }
        });



        // 新增一个timer
        mAddNewTimerLayout.setOnClickListener(new LinearLayout.OnClickListener()
        {
            public void onClick(View v)
            {
                // 生成一个timer，并初始化
                startTimerSettingActivity(addTimer(), REQUEST_CODE_TIMER_SETTING);
            }

        });
        // 新增一个timer
        mTvNoTimer.setOnClickListener(new LinearLayout.OnClickListener()
        {
            public void onClick(View v)
            {
                // 生成一个timer，并初始化
                startTimerSettingActivity(addTimer(), REQUEST_CODE_TIMER_SETTING);
            }

        });
    }

    /**
     * 创建PopupWindow
     */
    protected void initPopupWindow()
    {


    }

    public Timer addTimer()
    {
        Timer t = new Timer();

        t.setTimerDef(QuickSetting.setHourMinute(7, 00));

        t.setLoopPolicy(QuickSetting.weekCycle(new char[]{'0','1','1','1','1','1','0'}));
        t.setAction(QuickSetting.klaxon());
        t.setAction(QuickSetting.dialog());

        return t;
    }

    public void deleteTimer(int timer_id)
    {
        Util.log("delete timer: " + timer_id);

        TimerMgr.deleteTimer(timer_id);
        TimerMgr.setNextTimer(this, timer_id);

        for (int i = 0; i < mTimerItemList.size(); i++)
        {
            Timer t = (Timer) mTimerItemList.get(i).get("timer");
            if (t.getID() == timer_id)
            {
                mTimerItemList.remove(i);
            }
        }

        mTimerItemAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Util.log("requestCode = " + requestCode);
        if (RESULT_OK != resultCode)
        {
            return;
        }

        switch (requestCode)
        {
        case REQUEST_CODE_TIMER_SETTING:
            Timer timer = data.getParcelableExtra(TimerMgr.ALARM_INTENT_EXTRA);
            Util.log("MagicTimerActivity.onActivityResult:" + timer.getTimerDef().getDescription()
                    + " " + timer.getTimerDef().getName());
            Vector<Timer> timers = TimerMgr.getAllTimers();
            if (timer != null)
            {
                int i = 0;
                for (; i < timers.size(); i++)
                {
                    if (timers.get(i).getID() == timer.getID())
                    {
                        timers.set(i, timer);
                        break;
                    }
                }

                if (i == timers.size())
                {
                    timers.add(timer);
                }

                TimerMgr.setNextTimer(this, timer.getID());
            }
            // 更新列表
            for (int i = 0; i < mTimerItemList.size(); i++)
            {
                mTimerItemList.get(i).put("visibility", View.GONE);
            }
            this.updateTimerItemLayout(timer);
            break;
        default:
            break;
        }

        // updateLayout();
    }

    @Override
    public void onResume()
    {
        super.onResume();

        /**
         * 此处调用基本统计代码
         */
        StatService.onResume(this);
    }

    @Override
    public void onPause()
    {
        super.onPause();

        /**
         * 此处调用基本统计代码
         */
        StatService.onPause(this);
    }

    @Override
    protected void onDestroy()
    {
        PayConnect.getInstance(this).close();
        
        DownloadProxy.getInstance().stopDownloadSvc();
        
        DBHelper.getIntance().close();
        super.onDestroy();

        //这里会导致DownloadSvc被杀死
//        System.exit(0);
    }

    public void displayToast(String str)
    {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    public void displayToastLong(String str)
    {
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    }

    // 点击两次返回按钮退出程序
    private boolean first = false;
    private java.util.Timer exitTimer = new java.util.Timer();

    class MyTask extends TimerTask
    {
        @Override
        public void run()
        {
            first = false;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if (first)
            {
                finish();
                // 改在onDestroy调用exit
                // System.exit(0);
            } else
            {
                first = true;
                Toast.makeText(this, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
                exitTimer.schedule(new MyTask(), DEFAULT_INTERNAL_TO_EXIT);
            }
        } 
        else if (keyCode == KeyEvent.KEYCODE_MENU)
        {
            toggle();
//            mPopupWindow.showAsDropDown(mBtnConfig, -15, 0);
        }

        return false;
    }

    @Override
    public void getMessage(int msgCode, Object obj)
    {
        Message msg = new Message();
        msg.what = msgCode;
        msg.obj = obj;
        mHandler.sendMessage(msg);
    }

    public Handler mHandler = new Handler()
    {

        public void handleMessage(Message msg)
        {
            try
            {
                if (MsgReceiver.MSG_EXCEPTION == msg.what)
                {
                    displayToastLong((String) msg.obj);
                } else if (MsgReceiver.MSG_DELETE_TIMER == msg.what)
                {
                    deleteTimer((Integer) msg.obj);
                } else if (MsgReceiver.MSG_UPDATE_TIMER == msg.what)
                {
                    Vector<Timer> timers = TimerMgr.getAllTimers();
                    for (Timer timer : timers)
                    {
                        MagicTimerActivity.this.updateTimerItemLayout(timer);
                    }
                }
            } catch (Exception e)
            {
                Util.log_ex(e);
                displayToastLong("程序异常");
            }
        }
    };

    // **************************************************************************//
    class ListenerTime implements OnClickListener
    {
        private Timer mTimer;
        private Context mContext;

        ListenerTime(Context context, Timer timer)
        {
            mTimer = timer;
            mContext = context;
        }

        public void setTag(Object o)
        {
            if (o instanceof Timer)
            {
                mTimer = (Timer) o;
            }
        }

        @Override
        public void onClick(View arg0)
        {
            if (mTimer.getTimerDef().isEnable() == 0)
            {
                mTimer.getTimerDef().setEnable(1);
            } else
            {
                mTimer.getTimerDef().setEnable(0);
            }
            DBHelper.getIntance().updateTimerDef(mTimer.getTimerDef());

            mTimerItemAdapter.notifyDataSetChanged();
            TimerMgr.setNextTimer(mContext, TimerMgr.SPEC_TIME_ID_NONE);
        }
    }

    class ListenerNameInfo implements OnClickListener
    {
        private Timer mTimer;
        @SuppressWarnings("unused")
        private Context mContext;

        ListenerNameInfo(Context context, Timer timer)
        {
            mContext = context;
            mTimer = timer;
        }

        public void setTag(Object o)
        {
            if (o instanceof Timer)
            {
                mTimer = (Timer) o;
            }
        }

        @Override
        public void onClick(View v)
        {
            startTimerSettingActivity(mTimer, REQUEST_CODE_TIMER_SETTING);
        }
    }
    
    //长按timer item
    class TimerLongClickListener implements OnLongClickListener 
    {
        private Timer mTimer;
        @SuppressWarnings("unused")
        private Context mContext;

        TimerLongClickListener(Context context, Timer timer) 
        {
            mContext = context;
            mTimer = timer;
        }
        
        public void setTag(Object o)
        {
            if(o instanceof Timer)
            {
                mTimer = (Timer)o;
            }
        }

        @Override
        public boolean onLongClick(View v)
        {
            Util.log("Long Click timer " + mTimer.getName());
                        
            for(int i = 0; i < mTimerItemList.size(); i++)
            {
                Timer timer = (Timer)mTimerItemList.get(i).get("timer");
                if(timer.getID() == mTimer.getID())
                {
                    mCurrentTimerPos = i;
                                        
                    //anchor Bottom
                    int[] location = new  int[2] ;
                    v.getLocationOnScreen(location);
                    int anchorBottom = location[1] + v.getHeight();
                    //分辨率
                    int displayBottom = getResources().getDisplayMetrics().heightPixels;
                    
                    int offset = anchorBottom + mTimerMenuHeight - displayBottom;
                    Util.log("offset: " + offset + "=" + anchorBottom + "+" + mTimerMenuHeight + "-" + displayBottom);
                    if(offset > 0)
                    {
                        mTimerMenu.showAsDropDown(v, 0, -offset);
                    }
                    else
                    {
                        mTimerMenu.showAsDropDown(v);
                    }
                    
                    return true;
                }
            }
            
            return false;
        }
    }

    class ListenerMore implements OnClickListener
    {
        private Timer mTimer;

        ListenerMore(Context context, Timer timer)
        {
            mTimer = timer;
        }

        public void setTag(Object o)
        {
            if (o instanceof Timer)
            {
                mTimer = (Timer) o;
            }
        }

        @Override
        public void onClick(View v)
        {
            for (int i = 0; i < mTimerItemList.size(); i++)
            {
                Timer timer = (Timer) mTimerItemList.get(i).get("timer");
                if (timer.getID() == mTimer.getID())
                {
                    if ((Integer) mTimerItemList.get(i).get("visibility") == View.VISIBLE)
                    {
                        mTimerItemList.get(i).put("visibility", View.GONE);
                    } else
                    {
                        mTimerItemList.get(i).put("visibility", View.VISIBLE);
                    }
                } else
                {
                    mTimerItemList.get(i).put("visibility", View.GONE);
                }
            }
            mTimerItemAdapter.notifyDataSetChanged();

        }
    }

}
